package com.ucs.daoDbImpl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
import java.util.List;

import com.ucs.dao.OrderDao;
import com.ucs.domain.Order;
import com.ucs.domain.OrderDetail;


public class OrderDaoImpl implements OrderDao {
	public Order createOrder(Order order){
		insertOrder(order);
		for (OrderDetail orderDetail : order.getOrderDetails()) {
			orderDetail.setOrderID(order.getOrderID());
			insertOrderDetail(orderDetail);
		}
		return order;
	}
	
	public Order updateOrder(Order o) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public List getOrders() {
		// TODO Auto-generated method stub
		return null;
	}

	private static final String saveOrderCmd = "{call spSaveOrder_m(?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?)}"; 
	private static void insertOrder(Order order){
		Connection conn = DBUtil.getDbConnection("ucs_order");
		try {
			CallableStatement cs = conn.prepareCall(saveOrderCmd);
			cs.registerOutParameter(1, Types.INTEGER);
			cs.registerOutParameter(2, Types.INTEGER);
			cs.setInt(3, order.getOrderType());
			cs.setInt(4, order.getTableID());
			cs.setInt(5, order.getUserID());
			cs.setInt(6, order.getStationNum());
			cs.setString(7, order.getRemark());
			cs.registerOutParameter(8, Types.TIMESTAMP);
			cs.setInt(9, order.getActive());
			cs.setInt(10, order.getSend());
			cs.setInt(11, order.getGuestNum());
			cs.setString(12, order.getTROUTD());
			cs.setString(13, order.getReference());
			cs.setInt(14, order.getPaymentType());
			cs.setString(15, order.getTableText());
			cs.setString(16, order.getTrackData());
			cs.executeUpdate();
			
			order.setOrderID(cs.getInt(1));
			order.setTicketNum(cs.getInt(2));
			cs.close();			
		} catch (Exception e) {
			throw new UCSException("Could not execute stored procedure: "+saveOrderCmd,e);
		}
		finally{
			try {
				conn.close();				
			} catch (Exception e2) {
				throw new UCSException("Could not close connection for database: ucs_order",e2);
			}
		}
	}

	private static final String saveOrderDetailCmd="{call spSaveOrderDetail_m(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}";
	public static void insertOrderDetail(OrderDetail orderDetail) {
		Connection conn = DBUtil.getDbConnection("ucs_order");
		try {
			CallableStatement cs = conn.prepareCall(saveOrderDetailCmd);
			cs.registerOutParameter(1, Types.INTEGER);
			cs.setInt(2, orderDetail.getOrderID());
			cs.setInt(3, orderDetail.getSequenceNum());
			cs.setInt(4, orderDetail.getSeat());
			cs.setInt(5, orderDetail.getItemID());
			cs.setInt(6, orderDetail.getOpenItemID());
			cs.setDouble(7, orderDetail.getItemQty());
			cs.setBigDecimal(8, orderDetail.getItemPrice());
			cs.setInt(9, orderDetail.getOptionListID());
			cs.setInt(10, orderDetail.getModifierListID());
			cs.setBoolean(11, orderDetail.getVoid());
			cs.setInt(12, orderDetail.getActive());
			cs.setInt(13, orderDetail.getSent());
			cs.setInt(14, orderDetail.getItemHalf());
			cs.setInt(15, orderDetail.getHold());
			cs.executeUpdate();
			
			orderDetail.setOrderDetailID(cs.getInt(1));
			cs.close();
		}
		catch (Exception e) {
			throw new UCSException("Could not execute stored procedure: "+saveOrderDetailCmd,e);
		}
		finally{
			try {
				conn.close();				
			} catch (Exception e2) {
				throw new UCSException("Could not close connection for database: ucs_order",e2);
			}
		}
	}
	public static void main(String[] args) {
//		System.out.println(getOrder(4));
	}

}
